<template>
    <div class="minimal-login">
      <div class="login-card">
        <h2 class="login-title">系统登录</h2>
        <el-form 
          ref="loginFormRef" 
          :model="loginForm"
          :rules="loginRules"
          class="login-form"
        >
          <el-form-item prop="username">
            <el-input
              v-model="loginForm.username"
              placeholder="用户名"
              prefix-icon="User"
              size="large"
            />
          </el-form-item>
          
          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              type="password"
              placeholder="密码"
              prefix-icon="Lock"
              size="large"
              show-password
            />
          </el-form-item>
          
          <el-button
            type="primary"
            size="large"
            class="login-btn"
            @click="submitForm"
            :loading="loading"
          >
            登 录
          </el-button>
  
          <div class="register-link">
            <span>没有账户？</span>
            <router-link to="/register">去注册</router-link>
          </div>
        </el-form>
      </div>
    </div>
  </template>
  
  <script setup>
  import { ref, reactive } from 'vue'
  import { useRouter } from 'vue-router'
  import { ElMessage } from 'element-plus'
  import axios from 'axios'
  
  const router = useRouter()
  
  const loginForm = reactive({
    username: '',
    password: ''
  })
  
  const loginRules = reactive({
    username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
    password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
  })
  
  const loading = ref(false)
  const loginFormRef = ref(null) 
  
  const submitForm = () => {
    loginFormRef.value.validate(async (valid) => {
      if (valid) {
        loading.value = true
        try {
          const response = await axios.post('/api/user/login', {
            username: loginForm.username,
            password: loginForm.password
          })
  
          if (response.data.code === 0) {
            ElMessage.success(response.data.message || '登录成功')
            localStorage.setItem('user', JSON.stringify(response.data.data))
            router.push('/user')
          } else {
            ElMessage.error(response.data.message || '登录失败')
          }
        } catch (error) {
          ElMessage.error(error.response?.data?.message || '请求失败，请稍后重试')
        } finally {
          loading.value = false
        }
      }
    })
  }
  </script>
  
  <style scoped>
  .minimal-login {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    background-color: #f5f7fa;
  }
  
  .login-card {
    width: 360px;
    padding: 40px;
    background: white;
    border-radius: 8px;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  }
  
  .login-title {
    text-align: center;
    margin-bottom: 30px;
    color: #333;
    font-size: 20px;
    font-weight: normal;
  }
  
  .login-form :deep(.el-form-item) {
    margin-bottom: 24px;
  }
  
  .login-btn {
    width: 100%;
    margin-top: 10px;
  }
  
  .register-link {
    margin-top: 16px;
    text-align: center;
    font-size: 14px;
    color: #606266;
  }
  
  .register-link a {
    color: #409eff;
    text-decoration: none;
    margin-left: 4px;
  }
  
  .register-link a:hover {
    text-decoration: underline;
  }
  </style>